/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domoticus;

import java.util.List;

public class MicroControler {

    DBConnectionCon conectionDB;
    
    Actuator actionTemp;
    Actuator actionLight;
    Sensor sensorTemp;
    Sensor sensorHumid;
    Sensor sensorLumi;
    private final static String userName = "samunake";
    static Integer feedId;
    EnumType sensorType;
    Boolean activated = false;

    public MicroControler() {

        this.conectionDB = new DBConnectionCon();
        sensorTemp = new TemperatureSensor(conectionDB);
        sensorHumid = new HumiditySensor(conectionDB);
        sensorLumi = new LightSensor(conectionDB);
        actionLight = new LightActuator();
        actionTemp = new TemperatureActuator();


    }

    public List<String> getRooms() {

        return conectionDB.feedsToString(conectionDB.getUsersFeeds(userName));
    }

    public List<String> getSensors(String room) {
        Integer feedId = getFeedId(room);
        return conectionDB.datastreamToString(conectionDB.getDatastreams(feedId));

    }

    private String getTemperature(Integer feedId) {
        String value = sensorTemp.getCurrentValue(userName, feedId);
        checkTempDomoticSystem(value);
        return value;

    }

    private String getLuminosity(Integer feedId) {
        String value = sensorLumi.getCurrentValue(userName, feedId);
        checkLightDomoticSystem(value);
        return value;

    }

    private String getHumidity(Integer feedId) {
        String value = sensorHumid.getCurrentValue(userName, feedId);
        
        return value;

    }
    
    private List<String> getRangeTemperature(){
        return null;
        
    }

    public String getCurrentValue(String sensorTypeg, String room) {
        String value = null;

        sensorType = EnumType.toType(sensorTypeg);
        switch (sensorType) {
            case Temperature:
                value = getTemperature(getFeedId(room));
                break;
            case Humedad:
                value = getHumidity(getFeedId(room));
                break;
            case Luminosity:
                value = getLuminosity(getFeedId(room));
                break;
            default:
                value = "Empty";
                break;
        }
        return value;
    }

    public Integer getFeedId(String room) {
        feedId = conectionDB.getFeedId(userName, room);
        return feedId;
    }

    public String getRangegValues(String sensorTypeg, String room, String start, String end, String duracion, String Intervalo) {
        
        String value = null;

        sensorType = EnumType.toType(sensorTypeg);
        switch (sensorType) {
            case Temperature:
                value = getTemperature(getFeedId(room));
                break;
            case Humedad:
                value = getHumidity(getFeedId(room));
                break;
            case Luminosity:
                value = getLuminosity(getFeedId(room));
                break;
            default:
                value = "Empty";
                break;
        }
        return value;
    }
    public void startDomiticSystem(Double valueMinTemp,Double valueMaxTemp,Double valueMinLight,Double valueMaxLight){
        
        activated = true;
        actionTemp = new TemperatureActuator(valueMinTemp,valueMaxTemp);
        actionLight = new LightActuator(valueMinLight, valueMaxLight);
       
    }
    public void stopDomoticSystem(){
        activated = false;
    }
    private void checkTempDomoticSystem(String value){
        if(activated.equals(true)){
            
            Double valueDouble = Double.parseDouble(value);
            if(actionTemp.getMaxValue()>= valueDouble){
                activateLight();
                
            }
            else if (actionTemp.getMinValue() <= valueDouble){
                lightOFF();
            }
                       
            
        }
    }
    private void checkLightDomoticSystem(String value){
        if(activated.equals(true)){
           
            Double valueDouble = Double.parseDouble(value);
            if(actionLight.getMaxValue()>= valueDouble){
                activateAir();
            }
            else if (actionLight.getMinValue() <= valueDouble){
               airOFF();
            }
                       
            
        }
    }
    public void activateLight(){
        actionLight.sentArduinoON();
        
        
    }
     public void activateAir(){
         
        actionTemp.sentArduinoON();
    }
     
    public void lightOFF(){
        actionTemp.sentArduinoOFF();
    }
    
    public void airOFF(){
        actionLight.sentArduinoOFF();
    }
     
    
    
    
    
}
